/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.ingsoc2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 *
 * @author sfreiman
 */
public class GeneroDAOMYSQL implements GeneroDAO {

    Connection conn;

    public GeneroDAOMYSQL(Connection myconn) {
        conn = myconn;
    }

    public boolean crear(Genero pp) throws SQLException {
        String query;

        query = "INSERT INTO genero (nombre) VALUES(?)";

        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setString(1, pp.getNombre());

        return pstmt.executeUpdate() > 0;

    }

    public boolean borrar(Genero pp) throws SQLException {

        String query;
        PreparedStatement pstst;
        query = "DELETE from genero WHERE id =?";

        pstst = conn.prepareStatement(query);
        pstst.setInt(1, pp.getId());
        return pstst.executeUpdate() == 1;
    }

    public boolean guardar(Genero pp) throws SQLException {
        String query;
        query = "UPDATE genero SET nombre=? WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(query);
        pstmt.setString(1, pp.getNombre());
        pstmt.setInt(2, pp.getId());
        
        return pstmt.executeUpdate() == 1;
    }

    public Collection<Genero> listar() throws SQLException {
        String query;
        Statement stst;
        ResultSet rst1;
        List listaDePeliculas = new ArrayList<Pelicula>();

        Genero nGenero;

        query = "SELECT * FROM genero";
        stst = conn.createStatement();
        rst1 = stst.executeQuery(query);
        while (rst1.next()) {
            nGenero = new Genero();
            nGenero.setNombre(rst1.getString("nombre"));
            nGenero.setId(rst1.getInt("id"));

            listaDePeliculas.add(nGenero);
        }

        return listaDePeliculas;
    }

}
